package com.hzlj.position.locate.dao;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fz.common.base.converter.QueryWrapperConverterPlus;
import com.fz.common.base.core.JsonUtils;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.hzlj.position.locate.common.common.AbnWarningTrackDTO;
import com.hzlj.position.locate.common.dto.abnProhibitAreasPosition.AbnProhibitAreasPositionQueryParamDTO;
import com.hzlj.position.locate.common.dto.abnProhibitAreasPosition.AbnProhibitAreasPositionQueryResultDTO;
import com.hzlj.position.locate.common.model.AbnProhibitAreasPosition;
import com.hzlj.position.locate.mapper.AbnProhibitAreasPositionMapper;
import com.hzlj.sqjz.jz.common.constant.JzJoin;
import com.hzlj.sqjz.jz.common.model.JzJbxx;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * 禁止区域-定位记录(AbnProhibitAreasPosition)表操作类
 *
 * @author lifh
 * @date 2023-03-28 10:20:37
 */
@Repository
public class AbnProhibitAreasPositionDao extends ServiceImpl<AbnProhibitAreasPositionMapper, AbnProhibitAreasPosition> {
    @Resource
    private AbnProhibitAreasPositionMapper abnProhibitAreasPositionMapper;


    /**
     * 查询列表：分页
     *
     * @param dto  查询条件
     * @param page 分页信息
     * @return 对象列表
     */
    public QueryPageResultDTO<AbnProhibitAreasPositionQueryResultDTO> pageAbnProhibitAreasPosition(AbnProhibitAreasPositionQueryParamDTO dto, PageParamDTO page) {
        return QueryWrapperConverterPlus.of(abnProhibitAreasPositionMapper)
                .innerJoin(JzJbxx.class, JzJoin.jz(AbnProhibitAreasPosition::getJzId))
                .page(AbnProhibitAreasPositionQueryResultDTO.class, dto, page);
    }

    public void updateWarningTrack(String id,
                                   AbnWarningTrackDTO track) {
        LambdaUpdateWrapper<AbnProhibitAreasPosition> updateWrapper = new UpdateWrapper<AbnProhibitAreasPosition>().lambda()
                .set(AbnProhibitAreasPosition::getWarningTrack, JsonUtils.obj2json(track))
                .eq(AbnProhibitAreasPosition::getId, id);
        this.update(updateWrapper);
    }

    public AbnWarningTrackDTO getAWarningTrack(String id) {
        List<AbnWarningTrackDTO> tracks = QueryWrapperConverterPlus.of(abnProhibitAreasPositionMapper)
                .list(AbnProhibitAreasPosition::getWarningTrack, null, null, q -> q
                        .eq(AbnProhibitAreasPosition::getId, id)
                );
        if (ObjectUtil.isNotEmpty(tracks)) {
            return tracks.get(0);
        }
        return null;
    }

}

